package com.zyn.java8.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 使用Lambda表达式和函数式接口Predicate
 * 
 * @author tongyufu
 *
 */
public class PredicateDemo {

    public static void main(String[] args) {
        List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
        PredicateDemo.filter(languages, str -> str.startsWith("J"));

        //Predicate允许组合两个以上的条件，它提供了类似于逻辑与和或的操作and(),or()和xor()
        Predicate<String> startWith = str -> str.startsWith("J");
        Predicate<String> length = str -> str.length() == 4;
        PredicateDemo.filter(languages, startWith.and(length));
    }

    public static void filter(List<String> names, Predicate<String> condition) {
        names.stream().filter(name -> condition.test(name)).forEach(name -> {
            System.out.print(name + "  ");
        });
        System.out.println();
    }
}
